package com.android.lovely.ui.activity

import android.os.Bundle
import androidx.core.view.isVisible
import androidx.lifecycle.lifecycleScope
import coil.Coil
import com.android.lovely.BuildConfig
import com.android.lovely.R
import com.android.lovely.api.service.userService
import com.android.lovely.const.Gif
import com.android.lovely.databinding.ActivitySetLovelyBinding
import com.android.lovely.ext.copy
import com.android.lovely.ext.startWebView
import com.android.lovely.ext.windowBackgroundNav
import com.android.lovely.ext.windowBackgroundStatus
import com.android.lovely.framework.BaseActivity
import com.android.lovely.framework.UIController.showToast
import com.android.lovely.framework.launchWithLoading
import com.android.lovely.manager.KVStore
import com.android.lovely.ui.dialog.BindEmailDialog
import com.android.lovely.ui.dialog.showLovelyDialog
import com.android.lovely.user.UserManager
import com.android.lovely.user.UserManager.registerAuthChange
import com.google.android.ump.ConsentDebugSettings
import com.google.android.ump.ConsentInformation
import com.google.android.ump.ConsentRequestParameters
import com.google.android.ump.UserMessagingPlatform
import com.google.firebase.ktx.Firebase
import com.google.firebase.messaging.ktx.messaging
import com.kotlin.extensions.bus.FlowBus
import com.kotlin.extensions.bus.post
import com.kotlin.extensions.context.ContextExt.startPage
import com.kotlin.extensions.context.ContextExt.startPageClearTask
import com.kotlin.extensions.view.ViewExt.addStatusBarMarginTop
import com.kotlin.extensions.view.ViewExt.clickable

class SetLovelyActivity : BaseActivity<ActivitySetLovelyBinding>() {
    override fun initView(savedInstanceState: Bundle?) {
        mBinding.llTop.addStatusBarMarginTop()
        mBinding.back.clickable { onBackPressedDispatcher.onBackPressed() }
        registerAuthChange {
            mBinding.userId.text = it.id.toString()
            mBinding.email.text = it.email
        }
        mBinding.version.text = BuildConfig.VERSION_NAME
        mBinding.userId.clickable {
            UserManager.authId.toString().copy(this)
            showToast(getString(R.string.copy_success))
        }
        mBinding.gif.isSelected = KVStore.enableGif
        mBinding.gif.clickable {
            it.isSelected = it.isSelected.not()
            KVStore.enableGif = it.isSelected
            Coil.reset()
            FlowBus.post(Gif())
        }
        mBinding.ads.isVisible = KVStore.showUMP
        mBinding.ads.clickable {
            showLoading()
            UserMessagingPlatform.getConsentInformation(this).reset()
            val debugSettings = ConsentDebugSettings.Builder(this)
                .setDebugGeography(ConsentDebugSettings.DebugGeography.DEBUG_GEOGRAPHY_EEA)
                .addTestDeviceHashedId("").build()
            val params = ConsentRequestParameters.Builder().apply {
                if (BuildConfig.DEBUG) setConsentDebugSettings(debugSettings)
            }.build()
            val consentInformation = UserMessagingPlatform.getConsentInformation(this)
            consentInformation.requestConsentInfoUpdate(this, params, {
                dismissLoading()
                UserMessagingPlatform.loadAndShowConsentFormIfRequired(this) {
                }
            }, {})
        }
        mBinding.email.clickable { BindEmailDialog().show(supportFragmentManager) }
        mBinding.term.clickable { startWebView(getString(R.string.set_term), BuildConfig.TERM) }
        mBinding.privacy.clickable { startWebView(getString(R.string.set_pri), BuildConfig.TERM) }
        mBinding.delete.clickable {
            deleteAccount()
        }
        mBinding.logout.clickable {
            showLovelyDialog {
                setDialogTitle(getString(R.string.logout_confirm))
                setPositiveButton{
                    UserManager.logout()
                    startPageClearTask<SplashActivity>("logout" to true)
                }
            }
        }
        mBinding.password.clickable {
            if (UserManager.userInfo?.email.isNullOrEmpty()) {
                showToast(getString(R.string.bind_email_must))
                return@clickable
            }
            startPage<EditPwdActivity>()
        }
    }

    private fun deleteAccount() {
        showLovelyDialog {
            setDialogTitle(getString(R.string.delete_user1))
            setTopClose(true)
            setTopClose(false)
            setPositiveButton(getString(R.string.cancel)) {
                dismiss()
            }
            setNegativeButton(getString(R.string.confirm)) {
                showLovelyDialog {
                    setDialogTitle(getString(R.string.delete_user2))
                    setTopClose(true)
                    setTopClose(false)
                    setNegativeButton(getString(R.string.readed)) {
                        lifecycleScope.launchWithLoading {
                            userService.deleteAccount()
                            UserManager.logout()
                            startPageClearTask<LoginActivity>()
                        }
                    }
                    setPositiveButton(getString(R.string.cancel))
                }
            }
        }
    }
}